package com.cqz.services;

import com.cqz.mapper.BookInfoMapper;
import com.cqz.mapper.OrderInfoMapper;
import com.cqz.dao.BookInfo;
import com.cqz.dao.OrderInfo;
import com.cqz.pojo.OrderAndBook;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * OrderService服务层
 */
@Service("orderService")
public class OrderService {
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private SqlSession sqlSession;

    @Autowired
    private BookInfoMapper bookInfoMapper;

    /**
     * 添加购物车信息（在订单记录中添加用户为user_id，图书为bid，状态为未付款的订单记录）
     * @param user_id
     * @param bookId
     * @return
     */
    public String addRecord(String user_id, String bookId) {
        int uid=Integer.parseInt(user_id);
        int bid=Integer.parseInt(bookId);
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setBid(bid);
        orderInfo.setUid(uid);
        orderInfo.setOstate("未付款");
        OrderInfo orderInfo1=orderInfoMapper.findByBIdAndUid(orderInfo);
        if(orderInfo1!=null){
            return "exist already";
        }else {
            BookInfo bookInfo=bookInfoMapper.selectByPrimaryKey(bid);
            orderInfo.setOamount(bookInfo.getBprice());
            orderInfo.setOprice(bookInfo.getBprice());
            orderInfo.setOstate("未付款");
            orderInfo.setOnum(1);
            long l = System.currentTimeMillis();
            //new日期对象
            Date date = new Date(l);
            orderInfo.setOdate(date);
            int i=orderInfoMapper.insertSelective(orderInfo);
            if(i<=0)return "failure";
            else return "success";
        }
    }

    /**
     * 添加订单信息
     * @param orderInfo
     * @return
     */
    public boolean addPayment(OrderInfo orderInfo) {
        int i=orderInfoMapper.insertSelective(orderInfo);
        if(i<1) return false;
        else return true;
    }

    /**
     * 查询uid的购物车信息
     * @param uid
     * @return
     */
    public List<OrderInfo> findByUid(int uid) {
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setUid(uid);
        orderInfo.setOstate("未付款");
        return sqlSession.selectList("findStateHasPay",orderInfo);
    }

    /**
     * 查询uid的订单信息
     * @param uid
     * @return
     */
    public List<OrderInfo> findByUidNotPay(int uid) {
        OrderInfo orderInfo=new OrderInfo();
        orderInfo.setUid(uid);
        orderInfo.setOstate("未付款");
        return sqlSession.selectList("findStateNotPay",orderInfo);
    }

    /**
     * 删除订单
     * @param orderInfo
     */
    public void deleteByBid(OrderInfo orderInfo) {
        orderInfoMapper.deleteByBid(orderInfo);
    }

    /**
     * 管理员查询所有订单（即除了未付款的订单以外的所有订单)
     * @param map
     * @return
     */
    public List<OrderAndBook> selectAllOrders(HashMap<String,Object> map) {
        return sqlSession.selectList("selectAllOrders",map);
    }

    /**
     * 管理员查询特定订单
     * @param map
     * @return
     */
    public List<OrderAndBook> selectKindOrders(HashMap<String, Object> map) {
        return sqlSession.selectList("selectkindOrders",map);
    }

    /**
     * 根据oid订单id得到订单信息
     * @param parseInt
     * @return
     */
    public OrderInfo findOrderByOid(int parseInt) {
        return orderInfoMapper.selectByPrimaryKey(parseInt);
    }

    /**
     * 根据oid更新订单信息
     * @param orderInfo
     * @return
     */
    public boolean updateOrder(OrderInfo orderInfo) {
        int i=orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
        if(i<=0) return false;
        else return true;
    }
}
